package smartclass.present;

import com.jeremyfeinstein.slidingmenu.example.R;

import smartclass.bussiness.StudentBussiness;
import smartclass.bussiness.TeacherBussiness;
import smartclass.data.FinalValues;
import smartclass.entity.StudentInfo;
import smartclass.entity.TeacherInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class RegisterActivity extends Activity {
	private Button btnRegister, btnCancel;
	private EditText edtId, edtName, edtUsername, edtPassword, edtPassGain,
			edtPosition, edtPhoneNumber, edtAddress;
	private TextView tvPosition;
	private int person_type = R.id.teacherRadio;
	private String position_class = null;
	private RadioGroup typeGroup, genderGroup;
	ArrayAdapter<CharSequence> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_activity);
		btnRegister = (Button) findViewById(R.id.btnOKregister);
		btnCancel = (Button) findViewById(R.id.btnCancelregister);
		edtId = (EditText) findViewById(R.id.edtRegisterId);
		edtName = (EditText) findViewById(R.id.edtRegisterName);
		edtUsername = (EditText) findViewById(R.id.edtRegisteruser);
		edtPassword = (EditText) findViewById(R.id.edtRegisterpassword);
		edtPassGain = (EditText) findViewById(R.id.edtRegisterRepassword);
		edtPosition = (EditText) findViewById(R.id.edtRegisterPosition);
		typeGroup = (RadioGroup) findViewById(R.id.radioType);
		genderGroup = (RadioGroup) findViewById(R.id.radioGender);
		edtPhoneNumber = (EditText) findViewById(R.id.edtRegisterNumberPhone);
		tvPosition = (TextView) findViewById(R.id.tvPosition);
		edtAddress = (EditText) findViewById(R.id.edtRegisterAddress);
		typeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == R.id.teacherRadio) {
					tvPosition.setText("Position*");
					position_class = null;
					edtPosition.setHint("Choose a item");
					person_type = R.id.teacherRadio;
				} else {
					tvPosition.setText("Name Class*");
					position_class = null;
					edtPosition.setHint("Choose a item");
					person_type = R.id.studentRadio;
				}
			}
		});
		edtPosition.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(
						RegisterActivity.this);
				switch (person_type) {

				case R.id.teacherRadio:

					builder.setTitle("Teacher")
							.setItems(R.array.teacher_category,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {

											switch (which) {

											case 0:
												position_class = "GV A";
												break;
											case 1:
												position_class = "GV B";

												break;
											case 2:
												position_class = "GV C";

												break;
											case 3:
												position_class = "GV D";

												break;
											default:
												break;
											}
											edtPosition.setText(position_class);
										}
									}).create().show();
					break;
				case R.id.studentRadio:

					builder.setTitle("Choose Class")
							.setItems(R.array.class_category,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {

											switch (which) {

											case 0:
												position_class = "Class 1";
												break;
											case 1:
												position_class = "Class 2";

												break;
											case 2:
												position_class = "Class 3";

												break;
											case 3:
												position_class = "Class 4";

												break;
											default:
												break;
											}
											edtPosition.setText(position_class);
										}
									}).create().show();

					break;
				default:
					break;
				}

			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FinalValues.displayNotification(getBaseContext(),
						"Register is canceled!");
				finish();
			}
		});

		btnRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				switch (person_type) {
				case R.id.teacherRadio:
					TeacherInfo teacher = new TeacherInfo();
					TeacherBussiness teacherBussiness = TeacherBussiness
							.getInstance();
					String item = edtId.getText().toString();
					if (!item.isEmpty()) {

						if (!teacherBussiness.checkIdAvailable(Integer
								.parseInt(item))) {
							FinalValues.displayNotification(getBaseContext(),
									"ID existed\nEnter difference ID");
							return;
						} else {
							teacher.setId(Integer.parseInt(item));
						}

					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your ID");
						edtId.requestFocus();
						return;
					}
					item = edtName.getText().toString();
					if (!item.isEmpty()) {
						teacher.setName(item);
					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your Name");
						edtName.requestFocus();
						return;
					}

					item = edtUsername.getText().toString();
					if (!item.isEmpty()) {
						teacher.setUserName(item);
					}

					switch (genderGroup.getCheckedRadioButtonId()) {
					case R.id.maleRadio:
						teacher.setGender("Male");

						break;
					case R.id.femaleRadio:
						teacher.setGender("Female");

						break;
					default:
						break;
					}

					item = edtPassword.getText().toString();
					if (!item.isEmpty()) {

					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your PassWord");
						edtPassword.requestFocus();
						return;
					}

					item = edtPassGain.getText().toString();
					if (!item.isEmpty()) {
						if (!item.equals(edtPassword.getText().toString())) {
							FinalValues.displayNotification(getBaseContext(),
									"Password is not available");
							return;
						} else {
							teacher.setPassword(item);
						}
					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Repeat Your PassWord");
						edtPassGain.requestFocus();
						return;
					}

					if (!edtPosition.getText().toString().startsWith("Choose")) {
						teacher.setPosition(position_class);
					} else {
						FinalValues.displayNotification(RegisterActivity.this,
								"Please choose Teacher's Position");
					}
					if (!edtAddress.getText().toString().isEmpty()) {
						teacher.setAddress(edtAddress.getText().toString());
					}
					if (teacherBussiness.addTeacher(teacher)) {
						FinalValues.displayNotification(getBaseContext(),
								"Register success");
						finish();
					}
					break;
				case R.id.studentRadio:

					StudentInfo student = new StudentInfo();
					StudentBussiness studentBussiness = StudentBussiness
							.getInstance();
					String item1 = edtId.getText().toString();
					if (!item1.isEmpty()) {
						if (!studentBussiness.checkIdAvailable(Integer
								.parseInt(item1))) {
							FinalValues.displayNotification(getBaseContext(),
									"ID existed\nEnter difference ID");
							return;
						} else {
							student.setId(Integer.parseInt(item1));
						}

					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your ID");
						edtId.requestFocus();
						return;
					}
					item1 = edtName.getText().toString();
					if (!item1.isEmpty()) {
						student.setName(item1);
					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your Name");
						edtName.requestFocus();
						return;
					}

					item1 = edtUsername.getText().toString();
					if (!item1.isEmpty()) {
						student.setUserName(item1);
					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your User Name");
						edtUsername.requestFocus();
						return;
					}
					switch (genderGroup.getCheckedRadioButtonId()) {
					case R.id.maleRadio:
						student.setGender("Male");
						break;
					case R.id.femaleRadio:
						student.setGender("Female");
						break;
					default:
						break;
					}

					item1 = edtPassword.getText().toString();
					if (!item1.isEmpty()) {

					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Enter Your PassWord");
						edtPassword.requestFocus();
						return;
					}

					item1 = edtPassGain.getText().toString();
					if (!item1.isEmpty()) {
						if (!item1.equals(edtPassword.getText().toString())) {
							FinalValues.displayNotification(getBaseContext(),
									"Password is not available");
							return;
						} else {
							student.setPassword(item1);
						}
					} else {
						FinalValues.displayNotification(getBaseContext(),
								"Please Repeat Your PassWord");
						edtPassGain.requestFocus();
						return;
					}

					student.setPhoneNumber(edtPhoneNumber.getText().toString());

					if (!edtPosition.getText().toString().isEmpty()) {
						student.setClassName(position_class);
						
					} else {
						FinalValues.displayNotification(RegisterActivity.this,
								"Please choose Student's Class");
						return;
					}
					if (!edtAddress.getText().toString().isEmpty()) {
						student.setAddress(edtAddress.getText().toString());
					}
					if (studentBussiness.addStudent(student)) {
						FinalValues.displayNotification(getBaseContext(),
								"Register success");
						finish();
					}
					break;
				default:
					break;
				}
			}
		});
	}
}
